/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-05-08 16:11:33
 * @LastEditors: Sunny Chen
 * @LastEditTime: 2024-07-30 22:05:28
 */
import { defineStore } from 'pinia'
import { displayByName } from "@/cesiumTools/effectController";

export const useLineData = defineStore('lineData', {
    state: () => {
        return {
            lineData: [],
            // 全局共享viewer
            viewer: null,
            //设置侧边栏显示隐藏
            sidebarShow: null,
        }
    },
    getters: {
        // 获取线路显示隐藏信息
        lineDisplay() {
            return this.lineData.map(item => ({ name: item.checked, id: item.id, name: item.name }))
        },
        // 获取全部信息
        allData() {
            return this.lineData
        },
        Viewer() {
            return this.viewer
        },
    },
    actions: {
        setData(data) {
            return new Promise((resolve, reject) => {
                this.lineData = data.length ? data : []
                resolve(data)
            })
        },
        setViewer(viewer) {
            return new Promise((resolve, reject) => {
                this.viewer = viewer
                resolve(viewer)
            })
        },
        setSidebarShow(show) {
            this.sidebarShow = show
        },
        // 控制全部图层显示隐藏
        controlAll(isShow) {
            const lineNames = []
            const stationNames = []
            this.lineData.forEach(item => {
                const { name, stationsList } = item
                lineNames.push(name)
                stationsList.forEach(station => {
                    const { name } = station
                    stationNames.push(name)
                })
            })
            // 控制地图元素
            displayByName(lineNames, stationNames, isShow);
        },
        displayLine(lineNames, isShow) {
            if (Array.isArray(lineNames) && this.lineData.length && !this.disable) {
              const stationNames = [];
              // 控制state数据
              this.lineData.forEach((item) => {
                const { stationsList, name } = item;
                if (lineNames.includes(name)) {
                  item.checked = isShow;
                  stationsList.forEach((station) => {
                    stationNames.push(station.name);
                  });
                }
              });
              // 控制地图元素
              displayByName(lineNames, stationNames, isShow);
            }
          },
          // 设置是否开启全局管理
          disableController(val) {
            this.disable = val;
          },
    },

})

export const useMeasureData=defineStore('measureDataStore',{
    state:()=>{
        return {
            measureData:[]
        }
    },
    getters:{
        // 获取全部信息
        allData(){
            return this.measureData
        }
    },
    actions:{
        setData(data){
            return new Promise((resolve,reject)=>{
                this.measureData=data.length?data:[]
                resolve(data)
            })
        },
        clearData(){
          return new Promise((resolve,reject)=>{
            const data=this.measureData.map(item=>{
              item.length && item.forEach(n=>n.measures.length=0)
              return item
            })
            this.measureData=data
            resolve(data)
          })
        }
    },
  })